/**
 * 作者: clz
 * 日期: 2025-04-10 21:21:27
 * 备注: 联盟建筑表信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUnionBuild;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UnionBuildModel extends BaseModel implements Model {
	public static final String LISTKEY = "union_id.build_id";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

	protected long id; // id
	protected long unionId; // 工会id
	protected int buildId; // 建筑id
	protected int exp; // 当前经验
	protected int level; // 当前等级
	protected long buildTime; // 建造完成时间
	protected long startTime; // 建造开始时间

	public void reset() {
	}

	protected String getKey() {
		return "UnionBuild." + unionId + "." + buildId;
	}

	protected String getListKey() {
		return "UnionBuild." + unionId;
	}

	protected String getListItemKey() {
		return buildId + "";
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("union_build"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	 * 根据入库类型更新
	 */
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	 * 实时入队列
	 */
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // id
		obj.put("unionId", unionId); // 工会id
		obj.put("buildId", buildId); // 建筑id
		obj.put("exp", exp); // 当前经验
		obj.put("level", level); // 当前等级
		obj.put("buildTime", buildTime); // 建造完成时间
		obj.put("startTime", startTime); // 建造开始时间
		return obj;
	}

	public PBUnionBuild.Builder takeInitBuilder() {
		PBUnionBuild.Builder sb = PBUnionBuild.newBuilder();
		if (exp != 0) {
			sb.setExp(exp);
		}
		if (level != 0) {
			sb.setLevel(level);
		}
		if (buildTime != 0) {
			sb.setBuildTime(buildTime);
		}
		if (startTime != 0) {
			sb.setStartTime(startTime);
		}
		return sb;
	}

	public PBUnionBuild.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUnionBuild.Builder sb = PBUnionBuild.newBuilder();
		if (isFieldUpdate("exp")) {
			sb.setExp(exp);
		}
		if (isFieldUpdate("level")) {
			sb.setLevel(level);
		}
		if (isFieldUpdate("buildTime")) {
			sb.setBuildTime(buildTime);
		}
		if (isFieldUpdate("startTime")) {
			sb.setStartTime(startTime);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public long getUnionId() {
		return unionId;
	}

	public void putUnionId(long unionId) {
		if (this.unionId != unionId) {
			this.unionId = unionId;
			updateRedisHashField(getKey(), "unionId", this.unionId);
		}
	}

	public void addUnionId(long value) {
		this.unionId += value;
		updateRedisHashField(getKey(), "unionId", this.unionId);
	}

	public int getBuildId() {
		return buildId;
	}

	public void putBuildId(int buildId) {
		if (this.buildId != buildId) {
			this.buildId = buildId;
			updateRedisHashField(getKey(), "buildId", this.buildId);
		}
	}

	public void addBuildId(int value) {
		this.buildId += value;
		updateRedisHashField(getKey(), "buildId", this.buildId);
	}

	public int getExp() {
		return exp;
	}

	public void putExp(int exp) {
		if (this.exp != exp) {
			this.exp = exp;
			updateRedisHashField(getKey(), "exp", this.exp);
		}
	}

	public void addExp(int value) {
		this.exp += value;
		updateRedisHashField(getKey(), "exp", this.exp);
	}

	public int getLevel() {
		return level;
	}

	public void putLevel(int level) {
		if (this.level != level) {
			this.level = level;
			updateRedisHashField(getKey(), "level", this.level);
		}
	}

	public void addLevel(int value) {
		this.level += value;
		updateRedisHashField(getKey(), "level", this.level);
	}

	public long getBuildTime() {
		return buildTime;
	}

	public void putBuildTime(long buildTime) {
		if (this.buildTime != buildTime) {
			this.buildTime = buildTime;
			updateRedisHashField(getKey(), "buildTime", this.buildTime);
		}
	}

	public void addBuildTime(long value) {
		this.buildTime += value;
		updateRedisHashField(getKey(), "buildTime", this.buildTime);
	}

	public long getStartTime() {
		return startTime;
	}

	public void putStartTime(long startTime) {
		if (this.startTime != startTime) {
			this.startTime = startTime;
			updateRedisHashField(getKey(), "startTime", this.startTime);
		}
	}

	public void addStartTime(long value) {
		this.startTime += value;
		updateRedisHashField(getKey(), "startTime", this.startTime);
	}

}